package crond

import (
	"context"
	"device-admin/config/mysql"
	"device-admin/internal/app/grpc/service/terminal"
	"device-admin/internal/models"
	"strings"
	"time"

	"192.168.1.75/go-pkg/logx"
)

// AdvertisingPublish 广告投放
func AdvertisingPublish() {
	// 最近在线的设备
	var TerminalList = []models.Terminal{}
	mysql.NewDB().Model(&models.Terminal{}).
		Where("latest_active_time > ?", time.Now().Unix()-60).
		Select("robot_id, terminal_type").
		Order("latest_active_time").
		Find(&TerminalList)
	var deviceList = []string{}
	var TerminalTypeMap1 = map[string]string{
		"97": "1",
		"98": "2",
	}
	for _, v := range TerminalList {
		if _, ok := TerminalTypeMap1[v.TerminalType]; ok {
			deviceList = append(deviceList, v.RobotID+"_"+TerminalTypeMap1[v.TerminalType])

			if v.TerminalType == "97" {
				deviceList = append(deviceList, v.RobotID+"_3")
			}
		}
	}
	deviceParams := "" // 在线的设备 【设备 + '_' + 终端类型, 逗号分隔多个】
	if len(deviceList) > 0 {
		deviceParams = strings.Join(deviceList, ",")
	}
	logx.Info(context.Background(), "GetPublishSchedule in AdvertisingPublish:"+deviceParams)
	terminal.GetPublishSchedule(deviceParams, false, "AdvertisingPublish")
}
